

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/boshi.jpeg">
  <link rel="icon" type="image/png" href="/img/boshi.jpeg">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="wangpf">
  <meta name="keywords" content="">
  <title>Vue模板语法总结 - wpf的博客</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/foundation.min.css" />
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_6peoq002giu.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_pjno9b9zyxs.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.1.1"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>Wangpf's blog</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/img/article.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container page-header text-center fade-in-up">
            <span class="h2" id="subtitle">
              
            </span>

            
              
  <div class="mt-3 post-meta">
    <i class="iconfont icon-date-fill" aria-hidden="true"></i>
    <time datetime="2020-09-25 15:55" pubdate>
      2020年9月25日 下午
    </time>
  </div>


<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      6.1k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      92
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto" id="post">
            <!-- SEO header -->
            <h1 style="display: none">Vue模板语法总结</h1>
            
            <div class="markdown-body" id="post-body">
              <h1 id="模板语法⭐"><a href="#模板语法⭐" class="headerlink" title="模板语法⭐"></a>模板语法⭐</h1><h2 id="1-插值操作"><a href="#1-插值操作" class="headerlink" title="1.插值操作"></a>1.插值操作</h2><ul>
<li>如何将data中的文本数据，插入到HTML中呢？<ul>
<li>我们已经学习过了，可以通过Mustache语法(也就是双大括号)。</li>
<li>Mustache: 胡子/胡须.</li>
</ul>
</li>
<li>我们可以像下面这样来使用，并且数据是响应式的<ul>
<li><img src="https://i.loli.net/2020/09/25/leUTF7VAxQEdYmI.png" srcset="/img/loading.gif" alt="Mustache"></li>
</ul>
</li>
</ul>
<h3 id="1-1-on-once-的基本使用"><a href="#1-1-on-once-的基本使用" class="headerlink" title="1.1 on-once 的基本使用"></a>1.1 on-once 的基本使用</h3><ul>
<li>在某些情况下，我们可能不希望界面随意的跟随改变<ul>
<li>这个时候，我们就可以使用一个Vue的指令</li>
</ul>
</li>
</ul>
<div class="hljs"><pre><code class="hljs JavaScript">&lt;body&gt;
  &lt;div id=<span class="hljs-string">&quot;app&quot;</span>&gt;
    &lt;h2&gt;&#123;&#123;message&#125;&#125; &lt;/h2&gt;
    &lt;!-- v-once 指令 就是 不能修改该变量了 --&gt;
    &lt;!-- 该指令表示元素和组件只渲染一次，不会随着数据的改变而改变 --&gt;
    &lt;h2 v-once&gt;&#123;&#123;message&#125;&#125; &lt;/h2&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;script src=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;&lt;/script&gt;
&lt;script&gt;
  <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;
    el: <span class="hljs-string">&#x27;#app&#x27;</span>,
    data: &#123;
      message: <span class="hljs-string">&#x27;Hello Vue&#x27;</span>
    &#125;
  &#125;)
&lt;/script&gt;
</code></pre></div>

<p><img src="https://i.loli.net/2020/09/25/BmxRaKF34jDhCwe.png" srcset="/img/loading.gif" alt="image-20200919213449880"></p>
<h3 id="1-2-v-html的基本使用"><a href="#1-2-v-html的基本使用" class="headerlink" title="1.2 v-html的基本使用"></a>1.2 v-html的基本使用</h3><ul>
<li>某些情况下，我们从服务器请求到的数据本身就是一个HTML代码<ul>
<li>如果我们直接通过<code>双括号</code>来输出，会将HTML代码也一起输出。</li>
<li>但是我们可能希望的是按照HTML格式进行解析，并且显示对应的内容。</li>
</ul>
</li>
<li>如果我们希望解析出HTML展示<ul>
<li>可以使用v-html指令</li>
<li>该指令后面往往会跟上一个string类型</li>
<li>会将string的html解析出来并且进行渲染</li>
</ul>
</li>
</ul>
<div class="hljs"><pre><code class="hljs JavaScript">&lt;body&gt;
  &lt;div id=<span class="hljs-string">&quot;app&quot;</span>&gt;
    &lt;h2&gt;&#123;&#123;url&#125;&#125; &lt;/h2&gt;
    &lt;h2 v-html=<span class="hljs-string">&#x27;url&#x27;</span>&gt;&lt;/h2&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;script src=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;&lt;/script&gt;
&lt;script&gt;
  <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;
    el: <span class="hljs-string">&#x27;#app&#x27;</span>,
    data: &#123;
      url: <span class="hljs-string">&#x27;&lt;a href=&quot;https://www.baidu.com&quot;&gt;百度一下&lt;/a&gt;&#x27;</span>
    &#125;
  &#125;)
&lt;/script&gt;</code></pre></div>



<p><img src="https://i.loli.net/2020/09/25/Mie5l2Iug38jbBf.png" srcset="/img/loading.gif" alt="image-20200919213423736"></p>
<h3 id="1-3-v-text-的基本用法"><a href="#1-3-v-text-的基本用法" class="headerlink" title="1.3 v-text 的基本用法"></a>1.3 v-text 的基本用法</h3><ul>
<li>v-text作用和Mustache比较相似：都是用于将数据显示在界面中</li>
<li>v-text通常情况下，接受一个string类型</li>
<li></li>
</ul>
<div class="hljs"><pre><code class="hljs JavaScript">&lt;body&gt;
  &lt;div id=<span class="hljs-string">&quot;app&quot;</span>&gt;
    &lt;h2&gt;&#123;&#123;message&#125;&#125;,haha &lt;/h2&gt;
    &lt;!-- 会覆盖 hahha 不常用v-text  --&gt;
    &lt;h2 v-text=<span class="hljs-string">&#x27;message&#x27;</span>&gt;hahha&lt;/h2&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;script src=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;&lt;/script&gt;
&lt;script&gt;
  <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;
    el: <span class="hljs-string">&#x27;#app&#x27;</span>,
    data: &#123;
      message: <span class="hljs-string">&#x27;hello vue&#x27;</span>
    &#125;
  &#125;)
&lt;/script&gt;</code></pre></div>





<h3 id="1-4-v-pre-的基本使用"><a href="#1-4-v-pre-的基本使用" class="headerlink" title="1.4 v-pre 的基本使用"></a>1.4 v-pre 的基本使用</h3><ul>
<li>v-pre用于跳过这个元素和它子元素的编译过程，用于显示原本的Mustache语法。</li>
<li>比如下面的代码：<ul>
<li>第一个p标签中的内容会被编译解析出来对应的内容</li>
<li>第二个p标签中会直接显示</li>
</ul>
</li>
</ul>
<p><img src="https://i.loli.net/2020/09/25/7F1aZ5Y92KXQCoc.png" srcset="/img/loading.gif" alt="image-20200919213843348"></p>
<div class="hljs"><pre><code class="hljs JavaScript">&lt;body&gt;
  &lt;div id=<span class="hljs-string">&quot;app&quot;</span>&gt;
    &lt;h2&gt;&#123;&#123;message&#125;&#125; &lt;/h2&gt;
    &lt;!-- 不解析  message  只显示  &#123;&#123;message&#125;&#125; 这个文本 --&gt;
    &lt;h2 v-pre&gt;&#123;&#123;message&#125;&#125;&lt;/h2&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;script src=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;&lt;/script&gt;
&lt;script&gt;
  <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;
    el: <span class="hljs-string">&#x27;#app&#x27;</span>,
    data: &#123;
      message: <span class="hljs-string">&#x27;hello vue&#x27;</span>
    &#125;
  &#125;)
&lt;/script&gt;</code></pre></div>



<h3 id="1-5-v-cloak-的基本使用"><a href="#1-5-v-cloak-的基本使用" class="headerlink" title="1.5 v-cloak 的基本使用"></a>1.5 v-cloak 的基本使用</h3><ul>
<li>在某些情况下，我们浏览器可能会直接显然出未编译的Mustache标签。</li>
<li>cloak: 斗篷</li>
<li><strong>在刷新之前不希望把 {message}} 以这种形式显示出来</strong></li>
</ul>
<div class="hljs"><pre><code class="hljs JavaScript">&lt;style&gt;
  [v-cloak] &#123;
    display: none;
  &#125;
&lt;/style&gt;
&lt;!-- 在刷新之前不希望把 &#123;message&#125;&#125; 以这种形式显示出来 --&gt;

&lt;body&gt;
  &lt;div id=<span class="hljs-string">&quot;app&quot;</span>&gt;
    &lt;h2 v-cloak&gt;&#123;&#123;message&#125;&#125; &lt;/h2&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;script src=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;&lt;/script&gt;
&lt;script&gt;
  <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
    <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;
      el: <span class="hljs-string">&#x27;#app&#x27;</span>,
      data: &#123;
        message: <span class="hljs-string">&#x27;hello vue&#x27;</span>
      &#125;
    &#125;)
  &#125;, <span class="hljs-number">1000</span>)
&lt;/script&gt;</code></pre></div>

<h2 id="2-绑定属性-v-bind的使用"><a href="#2-绑定属性-v-bind的使用" class="headerlink" title="2. 绑定属性   v-bind的使用"></a>2. 绑定属性   v-bind的使用</h2><ul>
<li><p>除了内容需要动态来决定外，某些属性我们也希望动态来绑定。</p>
<ul>
<li>比如动态绑定a元素的href属性</li>
<li>比如动态绑定img元素的src属性</li>
</ul>
</li>
<li><p>这个时候，我们可以使用v-bind指令：</p>
<ul>
<li><strong>作用：动态绑定属性</strong></li>
<li><strong>缩写：<code>:</code></strong></li>
<li><strong>预期：any (with argument) | Object (without argument)</strong></li>
<li><strong>参数：attrOrProp (optional)</strong></li>
</ul>
</li>
</ul>
  <div class="hljs"><pre><code class="hljs JavaScript">&lt;body&gt;
  &lt;div id=<span class="hljs-string">&quot;app&quot;</span>&gt;
    &lt;!-- 错误 : 这里不能使用 Msutache 语法 urlImages --&gt;
    &lt;!-- <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;&#123;&#123;urlImage&#125;&#125;&quot;</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">&quot;&quot;</span>&gt;</span> --&gt;</span>
<span class="xml">    <span class="hljs-comment">&lt;!-- 使用 v-bind指令 来绑定属性 --&gt;</span></span>
<span class="xml">    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>&#123;&#123;message&#125;&#125; <span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span></span>
    &lt;img v-bind:src=&quot;urlImage&quot; alt=&quot;&quot;&gt;
    &lt;!-- v-bind 提供了一个语法糖 v-bind 对应的是 :  --&gt;
    &lt;img :src=&quot;urlImage&quot; alt=&quot;&quot;&gt;
  &lt;/div&gt;
<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span></span>
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
&lt;script&gt;
  const app = new Vue(&#123;
    el: &#x27;#app&#x27;,
    data: &#123;
      message: &#x27;hello vue&#x27;,
      urlImage: &#x27;https://i0.hdslb.com/bfs/space/cb1c3ef50e22b6096fde67febe863494caefebad.png@2200w_400h_1o.webp&#x27;
    &#125;
  &#125;)
<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span></code></pre></div>

<p>  <img src="https://i.loli.net/2020/09/25/6ANtm94IQCjJsvr.png" srcset="/img/loading.gif" alt="image-20200919215146368"></p>
<h3 id="2-1-v-bind-绑定-class-对象语法"><a href="#2-1-v-bind-绑定-class-对象语法" class="headerlink" title="2.1 v-bind 绑定 class  (对象语法)"></a>2.1 v-bind 绑定 class  (对象语法)</h3><ul>
<li><p>绑定方式：对象语法</p>
<ul>
<li>对象语法的含义是:class后面跟的是一个对象。</li>
<li>对象语法有下面这些用法：</li>
</ul>
</li>
</ul>
 <div class="hljs"><pre><code class="hljs HTML">用法一：直接通过&#123;&#125;绑定一个类
<span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">:class</span>=<span class="hljs-string">&quot;&#123;&#x27;active&#x27;: isActive&#125;&quot;</span>&gt;</span>Hello World<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>

用法二：也可以通过判断，传入多个值
<span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">:class</span>=<span class="hljs-string">&quot;&#123;&#x27;active&#x27;: isActive, &#x27;line&#x27;: isLine&#125;&quot;</span>&gt;</span>Hello World<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>

用法三：和普通的类同时存在，并不冲突
注：如果isActive和isLine都为true，那么会有title/active/line三个类
<span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;title&quot;</span> <span class="hljs-attr">:class</span>=<span class="hljs-string">&quot;&#123;&#x27;active&#x27;: isActive, &#x27;line&#x27;: isLine&#125;&quot;</span>&gt;</span>Hello World<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>

用法四：如果过于复杂，可以放在一个methods或者computed中
注：classes是一个计算属性
<span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;title&quot;</span> <span class="hljs-attr">:class</span>=<span class="hljs-string">&quot;classes&quot;</span>&gt;</span>Hello World<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
</code></pre></div>

<h3 id="2-2-v-bind-绑定-style"><a href="#2-2-v-bind-绑定-style" class="headerlink" title="2.2 v-bind 绑定 style"></a>2.2 v-bind 绑定 style</h3><p><strong>:style=”{color: currentColor, fontSize: fontSize + ‘px’}”</strong></p>
<ul>
<li>style后面跟的是一个对象类型<ul>
<li>对象的key是CSS属性名称</li>
<li>对象的value是具体赋的值，值可以来自于data中的属性</li>
</ul>
</li>
</ul>
<div class="hljs"><pre><code class="hljs JavaScript">&lt;body&gt;
  &lt;div id=<span class="hljs-string">&quot;app&quot;</span>&gt;
    &lt;!-- <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">:style</span>=<span class="hljs-string">&quot;&#123;CSS属性名:&#x27;value属性值&#x27;&#125;&quot;</span>&gt;</span>&#123;&#123;message&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span></span> --&gt;
    &lt;h2 :style=<span class="hljs-string">&quot;&#123;fontSize:&#x27;50px&#x27;,color:&#x27;red&#x27;&#125;&quot;</span>&gt;&#123;&#123;message&#125;&#125;&lt;/h2&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;script src=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;&lt;/script&gt;
&lt;script&gt;
  <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;
    el: <span class="hljs-string">&#x27;#app&#x27;</span>,
    data: &#123;
      message: <span class="hljs-string">&#x27;hello vue&#x27;</span>,
    &#125;
  &#125;)
&lt;/script&gt;</code></pre></div>



<h2 id="3-计算属性"><a href="#3-计算属性" class="headerlink" title="3.计算属性"></a>3.计算属性</h2><h3 id="3-1什么是计算属性，为什么要使用它"><a href="#3-1什么是计算属性，为什么要使用它" class="headerlink" title="3.1什么是计算属性，为什么要使用它"></a>3.1什么是计算属性，为什么要使用它</h3><p><strong>模板内的表达式非常便利，但是设计它们的初衷是用于简单运算的。在模板中放入太多的逻辑会让模板过重且难以维护,例如：</strong></p>
<div class="hljs"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;example&quot;</span>&gt;</span>
  &#123;&#123; message.split(&#x27;&#x27;).reverse().join(&#x27;&#x27;) &#125;&#125;
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></code></pre></div>

<p><strong>在这个地方，模板不再是简单的声明式逻辑，你必须看一段时间才能意识到，这里是想要显示变量 <code>message</code> 的翻转字符串，当你想要在模板中多次引用此处的翻转字符串时，就会更加难以处理。</strong></p>
<ul>
<li>使用 方法 methods</li>
<li>使用 计算属性 compute</li>
</ul>
<h3 id="3-2-计算属性和方法的区别"><a href="#3-2-计算属性和方法的区别" class="headerlink" title="3.2 计算属性和方法的区别"></a>3.2 计算属性和方法的区别</h3><ul>
<li>计算属性是基于他们的响应式依赖进行缓存的，只在相关响应式依赖发生改变时，才会重新求值（也就是说，计算属性会把数据进行缓存）</li>
<li>而方法不会把数据进行缓存， 所以用计算属性效率会更高点</li>
</ul>
<p><strong>所以，对于任何复杂逻辑，都应该使用计算属性</strong></p>
<ul>
<li>在某些情况，我们可能需要对数据进行一些转化后再显示，或者需要将多个数据结合起来进行显示<ul>
<li>比如我们有firstName和lastName两个变量，我们需要显示完整的名称。</li>
<li>但是如果多个地方都需要显示完整的名称，我们就需要写多个 </li>
</ul>
</li>
</ul>
<div class="hljs"><pre><code class="hljs JavaScript">&lt;body&gt;
  &lt;div id=<span class="hljs-string">&quot;app&quot;</span>&gt;
    &lt;h2&gt;&#123;&#123;firstName + <span class="hljs-string">&#x27; &#x27;</span> + lastName&#125;&#125;&lt;/h2&gt;
    &lt;h2&gt;&#123;&#123;firstName&#125;&#125; &#123;&#123;lastName&#125;&#125;&lt;/h2&gt;
    &lt;h2&gt;&#123;&#123;getFullName()&#125;&#125;&lt;/h2&gt;
    &lt;h2&gt;&#123;&#123;fullName&#125;&#125;&lt;/h2&gt;

  &lt;/div&gt;
&lt;/body&gt;
&lt;script src=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;&lt;/script&gt;
&lt;script&gt;
  <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;
    el: <span class="hljs-string">&#x27;#app&#x27;</span>,
    data: &#123;
      firstName: <span class="hljs-string">&#x27;Lron&#x27;</span>,
      lastName: <span class="hljs-string">&#x27;Man&#x27;</span>
    &#125;,
    <span class="hljs-comment">// 推荐使用计算属性来操作，因为它会将这些数据进行缓存， 无论打印多少次，它只会调用一次</span>
    computed: &#123; <span class="hljs-comment">//  计算属性   注意 : 计算的是 属性,  所以这里面的属性 看成一个 对象(用名词形式来表达), 调用时候不用加小括号</span>
      fullName: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.firstName + <span class="hljs-string">&#x27; &#x27;</span> + <span class="hljs-built_in">this</span>.lastName

      &#125;
    &#125;,
    methods: &#123;
      <span class="hljs-comment">//  不会缓存， 所以有多少次就调用多少次，  没有 computed 划算</span>
      getFullName: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.firstName + <span class="hljs-string">&#x27; &#x27;</span> + <span class="hljs-built_in">this</span>.lastName
      &#125;
    &#125;
  &#125;)
&lt;/script&gt;</code></pre></div>

<h3 id="3-3-计算属性的setter和getter"><a href="#3-3-计算属性的setter和getter" class="headerlink" title="3.3 计算属性的setter和getter"></a>3.3 计算属性的setter和getter</h3><blockquote>
<p>每个计算属性都有 set 和 get 方法 ，但一般我们不用去设置 set 方法， 只用 get 的方法  只读属性</p>
</blockquote>
<div class="hljs"><pre><code class="hljs JavaScript">computed:&#123;
    fullName:&#123;
        get()&#123;&#125;,
        set()&#123;&#125;
    &#125;
&#125;
<span class="hljs-comment">// 由于不经常用set， 如果只用 get方法的话 可以直接简化的成下面的。 </span>
	fullName()&#123;
        
    &#125;</code></pre></div>



<h2 id="4-监听事件-v-on-，事件处理"><a href="#4-监听事件-v-on-，事件处理" class="headerlink" title="4.监听事件(v-on)，事件处理"></a>4.监听事件(v-on)，事件处理</h2><ul>
<li><p>在前端开发中，我们需要经常和用于交互。</p>
<ul>
<li>这个时候，我们就必须监听用户发生的时间，比如点击、拖拽、键盘事件等等</li>
<li>在Vue中如何监听事件呢？使用v-on指令</li>
</ul>
</li>
<li><p>v-on介绍</p>
<ul>
<li>作用：绑定事件监听器</li>
<li>缩写：@</li>
<li>预期：Function | Inline Statement | Object</li>
<li>参数：event</li>
</ul>
</li>
</ul>
<h3 id="4-1-v-on-基本语法"><a href="#4-1-v-on-基本语法" class="headerlink" title="4.1 v-on 基本语法"></a>4.1 v-on 基本语法</h3>  <div class="hljs"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>&#123;&#123; message &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">v-on:click</span>=<span class="hljs-string">&quot;onClick&quot;</span>&gt;</span>测试<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

      <span class="hljs-comment">&lt;!-- 技巧：原生的怎么写，只需要把 on 替换为 v-on: --&gt;</span>
      <span class="hljs-comment">&lt;!-- &lt;input type=&quot;text&quot; v-on:keydown=&quot;&quot;&gt; --&gt;</span>
      <span class="hljs-comment">&lt;!-- &lt;input type=&quot;text&quot; v-on:keyup=&quot;&quot;&gt; --&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;node_modules/vue/dist/vue.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>
<span class="javascript">      <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;</span>
<span class="javascript">        el: <span class="hljs-string">&quot;#app&quot;</span>,</span>
        data: &#123;
<span class="javascript">          message: <span class="hljs-string">&quot;Hello Vue.js!&quot;</span></span>
        &#125;,
        /**
         * 实例选项 methods，存储模板中使用的方法
         */
        methods: &#123;
   
          onClick() &#123;
<span class="javascript">            <span class="hljs-built_in">window</span>.alert(<span class="hljs-string">&quot;hello&quot;</span>);</span>
          &#125;
        &#125;
      &#125;);
    <span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></code></pre></div>

<h3 id="4-2-处理函数的-this-指向"><a href="#4-2-处理函数的-this-指向" class="headerlink" title="4.2 处理函数的 this 指向"></a>4.2 处理函数的 this 指向</h3><div class="hljs"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>&#123;&#123; message &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;onClick&quot;</span>&gt;</span>测试<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;onClick2&quot;</span>&gt;</span>测试2<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;node_modules/vue/dist/vue.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>
<span class="javascript">      <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;</span>
<span class="javascript">        el: <span class="hljs-string">&quot;#app&quot;</span>,</span>
        data: &#123;
<span class="javascript">          message: <span class="hljs-string">&quot;Hello Vue.js!&quot;</span></span>
        &#125;,
        methods: &#123;
          onClick() &#123;
<span class="javascript">            <span class="hljs-comment">// 事件处理函数中的 this 指向的是 Vue 实例对象，也就是 app</span></span>
<span class="javascript">            <span class="hljs-comment">// 注意：事件处理函数不能使用箭头函数定义</span></span>
<span class="javascript">            <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span> === app);</span>

<span class="javascript">            <span class="hljs-built_in">this</span>.message = <span class="hljs-string">&quot;hello&quot;</span>;</span>
          &#125;,

<span class="javascript">          <span class="hljs-comment">// 注意：箭头函数定义的 methods 其中的 this 指向的是 window</span></span>
<span class="javascript">          <span class="hljs-comment">// 除非你不需要在函数中访问 Vue 实例 this，你可以定义箭头函数</span></span>
<span class="javascript">          <span class="hljs-comment">// 何必呢？</span></span>
<span class="javascript">          onClick2: <span class="hljs-function">() =&gt;</span> &#123;</span>
<span class="javascript">            <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>);</span>
          &#125;
        &#125;
      &#125;);
    <span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></code></pre></div>

<h3 id="4-3-v-on-的参数传递问题"><a href="#4-3-v-on-的参数传递问题" class="headerlink" title="4.3 v-on 的参数传递问题"></a>4.3 v-on 的参数传递问题</h3><div class="hljs"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- 1.事件调用的方法没有参数 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;btn1()&quot;</span>&gt;</span>按钮1<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;btn1&quot;</span>&gt;</span>按钮1<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

    <span class="hljs-comment">&lt;!-- 2.在事件定义时，写方法时省略了小括号，但是方法本身是需要一个参数，</span>
<span class="hljs-comment">    这个时候，Vue会默认将浏览器生产的event事件对象作为参数传入到方法中 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;btn2(123)&quot;</span>&gt;</span>按钮2<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span> <span class="hljs-comment">&lt;!-- 123--&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;btn2()&quot;</span>&gt;</span>按钮2<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span> <span class="hljs-comment">&lt;!-- undefined--&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;btn2&quot;</span>&gt;</span>按钮2<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span> <span class="hljs-comment">&lt;!-- event事件对象--&gt;</span>

    <span class="hljs-comment">&lt;!-- 3.方法定义时，我们需要event对象，同时也需要其他对象 俩个参数不分先后 --&gt;</span>
    <span class="hljs-comment">&lt;!-- 在调用方式，如何手动的获取浏览器参数event对象： $event  --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;btn3(123,$event)&quot;</span>&gt;</span>按钮3<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>
<span class="javascript">  <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;</span>
<span class="javascript">    el: <span class="hljs-string">&#x27;#app&#x27;</span>,</span>
    data: &#123;

    &#125;,
    methods: &#123;
      btn1() &#123;
<span class="javascript">        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;btn1&#x27;</span>);</span>
      &#125;,
      btn2(event) &#123;
<span class="javascript">        <span class="hljs-built_in">console</span>.log(event);</span>
      &#125;,
      btn3(abc, event) &#123;
<span class="javascript">        <span class="hljs-built_in">console</span>.log(abc, event);</span>
      &#125;
    &#125;
  &#125;)
<span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></code></pre></div>



<h3 id="4-4-v-on-修饰符"><a href="#4-4-v-on-修饰符" class="headerlink" title="4.4  v-on 修饰符"></a>4.4  v-on 修饰符</h3><ul>
<li>.stop - 调用 event.stopPropagation()。</li>
<li>.prevent - 调用 event.preventDefault()。</li>
<li>.{keyCode | keyAlias} - 只当事件是从特定键触发时才触发回调。</li>
<li>.native - 监听组件根元素的原生事件。</li>
<li>.once - 只触发一次回调。</li>
</ul>
<div class="hljs"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span>
<span class="css">  <span class="hljs-selector-class">.my</span> &#123;</span>
    width: 100px;
    height: 100px;
    background-color: pink;
  &#125;
<span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h4</span>&gt;</span>.stop : 调用 event.stopPropagation() 阻止冒泡事件<span class="hljs-tag">&lt;/<span class="hljs-name">h4</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h4</span>&gt;</span>.prevent : 调用 event.preventDefault() 阻止默认事件<span class="hljs-tag">&lt;/<span class="hljs-name">h4</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h4</span>&gt;</span> .[keyCode|keyAlias] 只当事件是从待定键触发时才触发回调<span class="hljs-tag">&lt;/<span class="hljs-name">h4</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;my&quot;</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;divClick&quot;</span>&gt;</span>
      <span class="hljs-comment">&lt;!-- 使用 .stop 修饰符 能够阻止事件冒泡 --&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click.stop</span>=<span class="hljs-string">&quot;btnClick&quot;</span>&gt;</span>按钮<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">action</span>=<span class="hljs-string">&quot;aaa&quot;</span>&gt;</span>
      <span class="hljs-comment">&lt;!-- 使用  .prevent 修饰符 能够阻止默认事件 --&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;提交&quot;</span> @<span class="hljs-attr">click.prevent</span>=<span class="hljs-string">&quot;inpClick&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- 监听某个键盘的键帽 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> @<span class="hljs-attr">keyup.enter</span>=<span class="hljs-string">&quot;keyup&quot;</span>&gt;</span> <span class="hljs-comment">&lt;!-- 监听回车键  --&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>
<span class="javascript">  <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;</span>
<span class="javascript">    el: <span class="hljs-string">&#x27;#app&#x27;</span>,</span>
    data: &#123;
<span class="javascript">      message: <span class="hljs-string">&#x27;hello vue&#x27;</span>,</span>
    &#125;,
    methods: &#123;
      divClick() &#123;
<span class="javascript">        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;div&#x27;</span>);</span>
      &#125;,
      btnClick() &#123;
<span class="javascript">        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;btn&#x27;</span>);</span>
      &#125;,
      inpClick() &#123;
<span class="javascript">        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;inp&#x27;</span>);</span>
      &#125;,
      keyup() &#123;
<span class="javascript">        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;keyup&#x27;</span>);</span>
      &#125;
    &#125;
  &#125;)
<span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></code></pre></div>



<h2 id="5-条件判断"><a href="#5-条件判断" class="headerlink" title="5.条件判断"></a>5.条件判断</h2><h3 id="5-1-v-if-、-v-else-if-、-v-else"><a href="#5-1-v-if-、-v-else-if-、-v-else" class="headerlink" title="5.1 v-if 、 v-else-if 、 v-else"></a>5.1 v-if 、 v-else-if 、 v-else</h3><ul>
<li><p>v-if、v-else-if、v-else</p>
<ul>
<li>这三个指令与JavaScript的条件语句if、else、else if类似。</li>
<li>Vue的条件指令可以根据表达式的值在DOM中渲染或销毁元素或组件</li>
</ul>
</li>
</ul>
<h3 id="5-2-v-if-和-v-show-的区别"><a href="#5-2-v-if-和-v-show-的区别" class="headerlink" title="5.2 v-if 和 v-show 的区别"></a>5.2 v-if 和 v-show 的区别</h3><ul>
<li>v-if条件渲染，如果条件为 false ， 则不渲染元素<ul>
<li>true 渲染 DOM</li>
<li>false 不渲染 DOM</li>
</ul>
</li>
<li>v-show 条件显示，无论条件的真假始终都渲染元素<ul>
<li>true 渲染 DOM</li>
<li>false 渲染DOM ，但不显示（display：none）</li>
<li>不能和  v-else-if 、 v-else 结合使用</li>
</ul>
</li>
</ul>
<blockquote>
<p>一般来说， <code>v-if</code> 有更高的切换开销， 而 <code>v-show</code> 有更高的初始渲染开销。 因此、如果需要非常频繁地切换，则使用 <code>v-show</code> 较好； 如果在运行时条件很少改变，则使用<code>v-if</code> 较好</p>
</blockquote>
<div class="hljs"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-comment">&lt;!-- 当需要在显示和隐藏之间切换频繁时，使用v-show </span>
<span class="hljs-comment">       当只有一次切换时，通过使用v-if</span>
<span class="hljs-comment">  --&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- 当条件为false时， v-if压根不会有对应的元素在DOM中 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">v-if</span>=<span class="hljs-string">&quot;isShow&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;A&quot;</span>&gt;</span>&#123;&#123;message&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- 当条件为false时， v-show 仅仅是将元素的display属性设置为了none而已 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">v-show</span>=<span class="hljs-string">&quot;isShow&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;B&quot;</span>&gt;</span>&#123;&#123;message&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>
<span class="javascript">  <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;</span>
<span class="javascript">    el: <span class="hljs-string">&#x27;#app&#x27;</span>,</span>
    data: &#123;
<span class="javascript">      message: <span class="hljs-string">&#x27;hello vue&#x27;</span>,</span>
<span class="javascript">      isShow: <span class="hljs-literal">false</span></span>
    &#125;
  &#125;)
<span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></code></pre></div>



<h2 id="6-循环遍历"><a href="#6-循环遍历" class="headerlink" title="6.循环遍历"></a>6.循环遍历</h2><h3 id="6-1-v-for-遍历数组"><a href="#6-1-v-for-遍历数组" class="headerlink" title="6.1 v-for 遍历数组"></a>6.1 v-for 遍历数组</h3><div class="hljs"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- v-for 第一个参数是 数组中的元素， 第二个参数是 索引值 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;item in names&quot;</span>&gt;</span>&#123;&#123;item&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;(item,index) in names&quot;</span>&gt;</span>&#123;&#123;index&#125;&#125;.&#123;&#123;item&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>
<span class="javascript">  <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;</span>
<span class="javascript">    el: <span class="hljs-string">&#x27;#app&#x27;</span>,</span>
    data: &#123;
<span class="javascript">      names: [<span class="hljs-string">&#x27;张三&#x27;</span>, <span class="hljs-string">&#x27;李四&#x27;</span>, <span class="hljs-string">&#x27;王五&#x27;</span>]</span>
    &#125;
  &#125;)
<span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></code></pre></div>

<h3 id="6-2-检测数组更新"><a href="#6-2-检测数组更新" class="headerlink" title="6.2 检测数组更新"></a>6.2 检测数组更新</h3><p> <strong>这些常见的数组操作方法都会正常的进行视图更新：</strong></p>
<div class="hljs"><pre><code class="hljs JavaScript">push();
pop();
shift();
unshift();
splice();
sort();
reverse();</code></pre></div>

<p><strong>或者直接对数组进行重新赋值：</strong></p>
<div class="hljs"><pre><code class="hljs JavaScript">实例.数组 = 新的数组;</code></pre></div>



<h3 id="6-3-v-for-遍历对象"><a href="#6-3-v-for-遍历对象" class="headerlink" title="6.3 v-for 遍历对象"></a>6.3 v-for 遍历对象</h3><div class="hljs"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- 1.在遍历对象中，如果只是一个参数，那么获取的是 属性值 （vlaue） --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;value in info&quot;</span>&gt;</span>&#123;&#123;value&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>

    <span class="hljs-comment">&lt;!-- 2.如果有俩个参数， 第一是 属性值（value） 第二个是 属性名（key）  --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;(value,key) in info&quot;</span>&gt;</span>&#123;&#123;key&#125;&#125;:&#123;&#123;value&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>

    <span class="hljs-comment">&lt;!-- 3.如果有三个参数， 第一是 属性值（value） 第二个是 属性名（key） 第三个是 索引值（）index --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;(value,key,index) in info&quot;</span>&gt;</span>&#123;&#123;index&#125;&#125;--&#123;&#123;key&#125;&#125;:&#123;&#123;value&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>
<span class="javascript">  <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;</span>
<span class="javascript">    el: <span class="hljs-string">&#x27;#app&#x27;</span>,</span>
    data: &#123;
      info: &#123;
<span class="javascript">        name: <span class="hljs-string">&#x27;wpf&#x27;</span>,</span>
        age: 18,
<span class="javascript">        sex: <span class="hljs-string">&#x27;男&#x27;</span></span>
      &#125;
    &#125;
  &#125;)
<span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></code></pre></div>



<h3 id="6-4-v-for-和-key"><a href="#6-4-v-for-和-key" class="headerlink" title="6.4 v-for 和 key"></a>6.4 v-for 和 key</h3><p>为了给 Vue 一个提示，以便它能跟踪每个节点的身份，从而重用和重新排序现有元素，你需要为每项提供一个唯一 <code>key</code> 属性：</p>
<div class="hljs"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;item in items&quot;</span> <span class="hljs-attr">v-bind:key</span>=<span class="hljs-string">&quot;item.id&quot;</span>&gt;</span>
  <span class="hljs-comment">&lt;!-- 内容 --&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></code></pre></div>

<p>建议尽可能在使用 <code>v-for</code> 时提供 <code>key</code> attribute，除非遍历输出的 DOM 内容非常简单，或者是刻意依赖默认行为以获取性能上的提升。</p>
<p>因为它是 Vue 识别节点的一个通用机制，<code>key</code> 并不仅与 <code>v-for</code> 特别关联。后面我们将在指南中看到，它还具有其它用途。</p>
<blockquote>
<p>注意：</p>
<ul>
<li>不要使用对象或数组之类的非基本类型值作为 <code>v-for</code> 的 <code>key</code>。请用字符串或数值类型的值。</li>
<li>不要使用遍历索引 index 作为唯一的 key 值，会有问题</li>
<li>一般使用数据中能表示唯一的那个字段，例如 id</li>
</ul>
</blockquote>
<h2 id="7-表单绑定"><a href="#7-表单绑定" class="headerlink" title="7.表单绑定"></a>7.表单绑定</h2><ul>
<li>Vue中使用v-model指令来实现表单元素和数据的双向绑定。</li>
<li>案例的解析：<ul>
<li>当我们在输入框输入内容时</li>
<li>因为input中的v-model绑定了message，所以会实时将输入的内容传递给message，message发生改变。</li>
<li>当message发生改变时，因为上面我们使用Mustache语法，将message的值插入到DOM中，所以DOM会发生响应的改变。<br>所以，通过v-model实现了双向的绑定。</li>
</ul>
</li>
</ul>
<div class="hljs"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>&#123;&#123;message&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- 双向绑定 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;message&quot;</span>&gt;</span>
    &#123;&#123;message&#125;&#125;
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>
<span class="javascript">  <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;</span>
<span class="javascript">    el: <span class="hljs-string">&#x27;#app&#x27;</span>,</span>
    data: &#123;
<span class="javascript">      message: <span class="hljs-string">&#x27;hello vue&#x27;</span>,</span>
    &#125;
  &#125;)
<span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></code></pre></div>

<h3 id="7-1-v-model-原理"><a href="#7-1-v-model-原理" class="headerlink" title="7.1 v-model 原理"></a>7.1 v-model 原理</h3><ul>
<li>v-model 其实是一个语法糖，它的背后本质上是包含俩个操作：<ul>
<li>v-bind 绑定一个 value 属性</li>
<li>v-on 指令给当前元素绑定 input 事件</li>
</ul>
</li>
<li>也就是说下面的代码：等同于下面的代码：</li>
</ul>
<div class="hljs"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;message&quot;</span>&gt;</span>
等同于
<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">v-bind:value</span>=<span class="hljs-string">&quot;message&quot;</span> <span class="hljs-attr">v-on:input</span>=<span class="hljs-string">&quot;message = $event.target.value&quot;</span>&gt;</span></code></pre></div>



<h3 id="7-2-v-model-checkbox"><a href="#7-2-v-model-checkbox" class="headerlink" title="7.2 v-model : checkbox"></a>7.2 v-model : checkbox</h3><ul>
<li>复选框分为俩种情况：单个勾选框和多个勾选框</li>
<li>单个勾选框：<ul>
<li>v-model 即为布尔值。</li>
<li>此时input的value并不影响v-model的值。</li>
</ul>
</li>
<li>多个复选框：<ul>
<li>当是多个复选框时，因为可以选中多个，所以对应的data中属性是一个数组。</li>
<li>当选中某一个时，就会将input的value添加到数组中。</li>
</ul>
</li>
</ul>
<div class="hljs"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">&quot;agreement&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;agreement&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;isAgree&quot;</span>&gt;</span>同意协议
    <span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">:disabled</span>=<span class="hljs-string">&quot;!isAgree&quot;</span>&gt;</span>下一步<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- &lt;input type=&quot;checkbox&quot; v-model=&quot;hobbies&quot; value=&quot;足球&quot;&gt;足球</span>
<span class="hljs-comment">    &lt;input type=&quot;checkbox&quot; v-model=&quot;hobbies&quot; value=&quot;篮球&quot;&gt;篮球</span>
<span class="hljs-comment">    &lt;input type=&quot;checkbox&quot; v-model=&quot;hobbies&quot; value=&quot;羽毛球&quot;&gt;羽毛球</span>
<span class="hljs-comment">    &lt;input type=&quot;checkbox&quot; v-model=&quot;hobbies&quot; value=&quot;排球&quot;&gt;排球 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>你的爱好是：&#123;&#123;hobbies&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;item in originHobbies&quot;</span> <span class="hljs-attr">:for</span>=<span class="hljs-string">&quot;item&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">:id</span>=<span class="hljs-string">&quot;item&quot;</span> <span class="hljs-attr">:value</span>=<span class="hljs-string">&quot;item&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;hobbies&quot;</span>&gt;</span>&#123;&#123;item&#125;&#125;
    <span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>
<span class="javascript">  <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;</span>
<span class="javascript">    el: <span class="hljs-string">&#x27;#app&#x27;</span>,</span>
    data: &#123;
<span class="javascript">      message: <span class="hljs-string">&#x27;hello vue&#x27;</span>,</span>
<span class="javascript">      isAgree: <span class="hljs-literal">false</span>,</span>
      hobbies: [],
<span class="javascript">      originHobbies: [<span class="hljs-string">&#x27;足球&#x27;</span>, <span class="hljs-string">&#x27;篮球&#x27;</span>, <span class="hljs-string">&#x27;羽毛球&#x27;</span>, <span class="hljs-string">&#x27;排球&#x27;</span>]</span>
    &#125;
  &#125;)
<span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></code></pre></div>



<h3 id="7-3-v-model-：-select"><a href="#7-3-v-model-：-select" class="headerlink" title="7.3 v-model ： select"></a>7.3 v-model ： select</h3><ul>
<li>和checkbox一样，select也分单选和多选两种情况。</li>
<li>单选：只能选中一个值。<ul>
<li>v-model绑定的是一个值。</li>
<li>当我们选中option中的一个时，会将它对应的value赋值到mySelect中</li>
</ul>
</li>
<li>多选：可以选中多个值。<ul>
<li>v-model绑定的是一个数组。</li>
<li>当选中多个值时，就会将选中的option对应的value添加到数组mySelects中</li>
</ul>
</li>
</ul>
<p><img src="https://i.loli.net/2020/09/25/HrvkQnG3p6jBtzd.png" srcset="/img/loading.gif" alt="image-20200920162613143"></p>
<h3 id="7-4-v-model-修饰符的使用"><a href="#7-4-v-model-修饰符的使用" class="headerlink" title="7.4 v-model 修饰符的使用"></a>7.4 v-model 修饰符的使用</h3><div class="hljs"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- lazy（懒惰的） 修饰符 ： 默认情况下，v-model默认是在input事件中同步输入框的数据的。</span>
<span class="hljs-comment">    也就是说， 一旦有数据发生改变对应的data中的数据也会自动发生改变</span>
<span class="hljs-comment">    lazy修饰符可以让数据在失去焦点或者回车时才会更新 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">v-model.lazy</span>=<span class="hljs-string">&quot;message&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>&#123;&#123;message&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>


    <span class="hljs-comment">&lt;!-- 2. number 修饰符  v-model 默认会把数据以string的形式传递， 我们需要传递是数字型，这时就可以用 number修饰符 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;number&quot;</span> <span class="hljs-attr">v-model.number</span>=<span class="hljs-string">&quot;age&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>&#123;&#123;age&#125;&#125;--- &#123;&#123;typeof age&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>

    <span class="hljs-comment">&lt;!-- 3. trim (修剪) 修饰符：  如果输入的内容首尾有很多空格， 通常我们希望将其去除， 这是 trim 修饰符可以过滤内容左右俩边的空格 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">v-model.trim</span>=<span class="hljs-string">&quot;name&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>你的名字是：&#123;&#123;name&#125;&#125; 注意在这里（浏览器里默认空格去除了，但真实的数据没有，F12进行操作查看）<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>
<span class="javascript">  <span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Vue(&#123;</span>
<span class="javascript">    el: <span class="hljs-string">&#x27;#app&#x27;</span>,</span>
    data: &#123;
<span class="javascript">      message: <span class="hljs-string">&#x27;hello vue&#x27;</span>,</span>
      age: 18,
<span class="javascript">      name: <span class="hljs-string">&#x27;&#x27;</span></span>
    &#125;
  &#125;)
<span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></code></pre></div>






            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                <div class="post-meta mr-3">
                  <i class="iconfont icon-category"></i>
                  
                  <a class="hover-with-bg" href="/categories/vue/">vue</a>
                  
                </div>
                
                
                <div class="post-meta">
                  <i class="iconfont icon-tags"></i>
                  
                  <a class="hover-with-bg" href="/tags/vue/">vue</a>
                  
                </div>
                
              </div>
              
              
              <div class="post-prevnext row">
                <article class="post-prev col-6">
                  
                  
                  <a href="/2020/09/25/19-Vue%E7%BB%84%E4%BB%B6%E5%8C%96%E5%BC%80%E5%8F%91/">
                    <i class="iconfont icon-arrowleft"></i>
                    <span class="hidden-mobile">Vue组件化开发</span>
                    <span class="visible-mobile">上一篇</span>
                  </a>
                  
                </article>
                <article class="post-next col-6">
                  
                  
                  <a href="/2020/09/02/17.filter-%E3%80%81map-%E3%80%81reduce-%E5%87%BD%E6%95%B0%E7%9A%84%E4%BD%BF%E7%94%A8/">
                    <span class="hidden-mobile">filter()、map()、reduce()函数的使用</span>
                    <span class="visible-mobile">下一篇</span>
                    <i class="iconfont icon-arrowright"></i>
                  </a>
                  
                </article>
              </div>
              
            </div>

            
            <!-- Comments -->
            <article class="comments" id="comments">
              
              
  <div id="vcomments"></div>
  <script type="text/javascript">
    function loadValine() {
      addScript('https://cdn.staticfile.org/valine/1.4.14/Valine.min.js', function () {
        new Valine({
          el: "#vcomments",
          app_id: "hjxsDRXYScicliqD8I8JqoTu-gzGzoHsz",
          app_key: "qUcAEReEqeEVp5wwXJ41uCCp",
          placeholder: "说点什么",
          path: window.location.pathname,
          avatar: "retro",
          meta: ["nick","mail","link"],
          pageSize: "10",
          lang: "zh-CN",
          highlight: true,
          recordIP: false,
          serverURLs: "",
        });
      });
    }
    waitElementVisible('vcomments', loadValine);
  </script>
  <noscript>Please enable JavaScript to view the <a target="_blank" href="https://valine.js.org" rel="nofollow noopener noopener">comments
      powered by Valine.</a></noscript>


            </article>
            
          </article>
        </div>
      </div>
    </div>
    
    <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
      <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

    </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  

  <footer class="mt-5">
  <div class="text-center py-3">
    <div>
      <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a>
      <i class="iconfont icon-love"></i>
      <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener">
        <span>Fluid</span></a>
    </div>
    
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


    

    
  </div>
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    
      <script  src="/js/lazyload.js" ></script>
    
  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: '#post-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "Vue模板语法总结&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "always",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>







  
  
    <script>
      !function (e, t, a) {
        function r() {
          for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");
          requestAnimationFrame(r)
        }

        function n() {
          var t = "function" == typeof e.onclick && e.onclick;
          e.onclick = function (e) {
            t && t(), o(e)
          }
        }

        function o(e) {
          var a = t.createElement("div");
          a.className = "heart", s.push({
            el: a,
            x: e.clientX - 5,
            y: e.clientY - 5,
            scale: 1,
            alpha: 1,
            color: c()
          }), t.body.appendChild(a)
        }

        function i(e) {
          var a = t.createElement("style");
          a.type = "text/css";
          try {
            a.appendChild(t.createTextNode(e))
          } catch (t) {
            a.styleSheet.cssText = e
          }
          t.getElementsByTagName("head")[0].appendChild(a)
        }

        function c() {
          return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
        }

        var s = [];
        e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {
          setTimeout(e, 1e3 / 60)
        }, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r()
      }(window, document);
    </script>
  











  

  

  

  

  

  





</body>
</html>
